package GPSReader;

import java.util.ArrayList;
import ocss.nmea.api.NMEAReader;
import app.*;
import gpsmap.RouteDrawer;
import gpsmap.WaypointPainter;
import javax.swing.JOptionPane;
/**
 * 
 * @author Amir Tayarani, Navid Ranjbar
 */
public class GPSConnectionString extends NMEAReader implements Runnable {

    static Parameters setting;
    static Com connection;
    public static double longitude;
    public static double latitude;
    private boolean alert = true;

    public GPSConnectionString(ArrayList al) {
        super(al);
//        CurrentPosition cp = new CurrentPosition();
    }
    /** this thread which is basicaly the performs reading from gps device and extracting the data for current location
     * author Amirhossein Tayarani, Navid Ranjbar
     */
    @Override
    public void run() {
        try {
            setting = new Parameters();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            setting.setPort("COM4");
            setting.setBaudRate("4800");
            setting.setParity("N");
            alert = true;
        } catch (Exception e) {
            if (alert) {
                JOptionPane.showMessageDialog(null, " Please Connect GPS Device to COM4 ", "NOT FOUND On COM4", 1);
            }
            alert = false;
        }
        try {
            connection = new Com(setting);
        } catch (Exception e) {

//            e.printStackTrace();
        }
        String GPSString = "";
        char temp;
        String[] spilitString = null;

        while (true) {
            try {

                GPSString = "";
                try {
                    while (connection.receiveSingleChar() != '$') {
                        continue;
                    }
              
                GPSString += "$";
                temp = connection.receiveSingleChar();
                while (temp != '\n') {
                    if (GPSString.length() > 2000483647) {
                        GPSString = "";
                    }
                    GPSString += temp;
                    temp = connection.receiveSingleChar();
                }
                GPSString += "\n";

                if (GPSString.substring(0, 7).equals("$GPRMC,")) {
                    spilitString = GPSString.split(",");
                }
                  } catch (Exception e) {
                    if (alert) {
                        JOptionPane.showMessageDialog(null, " Please Connect GPS Device to COM4 ", "NOT FOUND On COM4", 1);
                    }
                    alert = false;
                }
                try {
                    latitude = (Double.parseDouble(spilitString[3]) / 100);
                    latitude = ((latitude - (latitude - (int) (latitude))) + 100 * (latitude - (int) (latitude)) / 60);
                    longitude = (Double.parseDouble(spilitString[5]) / 100);
                    longitude = ((longitude - (longitude - (int) (longitude))) + 100 * (longitude - (int) (longitude)) / 60);

//                System.out.println(latitude);
//                System.out.println(longitude);

                } catch (Exception e) {
//                    e.getMessage();
//                    System.out.println("");
                }

                RouteDrawer routerDrawer = new RouteDrawer();
                if (latitude != 0.0 && longitude != 0.0) {
                    routerDrawer.setStartPoint(latitude, longitude);
                }
                WaypointPainter wpPaint = new WaypointPainter();
                wpPaint.setStarttWaypoint(latitude, longitude);
                Thread.sleep(1000);
            } catch (Exception e) {

                e.printStackTrace();
            }
        }

        //System.out.print(GPSString);
    }

    @Override
    public void read() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
//    @Override
//    public void read() {
//        throw new UnsupportedOperationException("Not supported yet.");
//    }
//        public void run() {
//            try {
//
//                while(true){
//
//                     Thread.sleep(2000);
//                }
//            } catch (InterruptedException ex) {
//                Logger.getLogger(GPSConnectionString.class.getName()).log(Level.SEVERE, null, ex);
//            }
//
//            throw new UnsupportedOperationException("Not supported yet.");
//        }
//    };
//    Thread readtheard;
//    @Override
//    public void run(){
//
//        readtheard = new Thread(this);
//        readtheard.start();
//
//    }
//    public static void main(String[] args) throws Exception {
//
//        reader();
//    }
}



